package com.frame.model;

import java.util.List;

import com.frame.hander.SearchParamHander;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * @author LRC
 * @createDate 2015-9-11
 * @description miniui列表查询参数封装 
 */
public class MiniPager {
	public int total;
	public List data;
	public int pageIndex;
	public int pageSize;
	public String sortField;
	public String sortOrder;
	public List<Object> params;//参数列表
	/**
	 * @updateTeim 2015年6月5日下午2:59:24
	 * @description 获取miniui分页查询参数对象
	 * @param c
	 * @return
	 */
	public static MiniPager getMiniPager(Controller c){
		MiniPager pager = new MiniPager();
		pager.pageIndex = c.getParaToInt("pageIndex") + 1;
		pager.pageSize = c.getParaToInt("pageSize");
		pager.sortField = c.getPara("sortField");
		pager.sortOrder = c.getPara("sortOrder");
		return pager;
	}
	public static MiniPager getMiniPager(Controller c,String[] paramNames,
			StringBuilder sql){
		MiniPager pager = new MiniPager();
		pager.pageIndex = c.getParaToInt("pageIndex") + 1;
		pager.pageSize = c.getParaToInt("pageSize");
		pager.sortField = c.getPara("sortField");
		pager.sortOrder = c.getPara("sortOrder");
		if(paramNames!=null){
			pager.params = SearchParamHander.getSQLParams(paramNames, c, sql, null);
		}
		if(pager.sortField!=null&&!"".equals(pager.sortField)){
			sql.append(" order by " +pager.sortField + " " + pager.sortOrder);
		}
		return pager;
	}
	/**
	 * @updateTeim 2015年6月5日下午3:13:39
	 * @description 执行查询操作 
	 */
	public void execQuery(String select,String from){
		Page<Record> fpager = null;
		if(params==null||params.size()==0){
			fpager = Db.paginate(pageIndex, pageSize, 
					select, from);
		}else{
			fpager = Db.paginate(pageIndex, pageSize, 
					select, from, params.toArray());
		}
		this.total = fpager.getTotalRow();
		this.data = fpager.getList();
	}
	public int getPageIndex() {
		return pageIndex;
	}
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public String getSortField() {
		return sortField;
	}
	public void setSortField(String sortField) {
		this.sortField = sortField;
	}
	public String getSortOrder() {
		return sortOrder;
	}
	public void setSortOrder(String sortOrder) {
		this.sortOrder = sortOrder;
	}
	public List<Object> getParams() {
		return params;
	}
	public void setParams(List<Object> params) {
		this.params = params;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public List<Object> getData() {
		return data;
	}
	public void setData(List<Object> data) {
		this.data = data;
	}
}
